app.controller('goodsFileUploadCtrl', ['$scope','$window', 'FileUploader','$cookies','$stateParams','toaster','goodsFileUploadService','goodsDetailPicturesService', function($scope,$window, FileUploader,$cookies,$stateParams,toaster,goodsFileUploadService,goodsDetailPicturesService) {

    $scope.uploadStatus = $scope.uploadStatus1 = false;
    $scope.goodsId=$stateParams.goodsId;
    $scope.queueLimit=1;
    $scope.Imagestype=-1;
    $scope.goodsImages=new Array();
    $scope.goodsDetailPicturesService=goodsDetailPicturesService;
    $scope.index=0;

    var uploader = $scope.uploader = new FileUploader({
        url: $scope.app.domain+'/upload',
        formData:[{sessionId:$cookies.sessionId}]
    });

    // FILTERS
    uploader.filters.push({
        name: 'picFilter',
        fn: function(item, options) {
            if(!(item.type=='image/png' ||item.type=='image/gif' || item.type=='image/jpeg' || item.type=='image/jpg')){
                toaster.pop('waiting', "添加图片失败", "不属于图片类型");
                return false;
            }

            if(item.size>2*1024*1024){
                toaster.pop('waiting', "添加图片失败", "文件太大,超出2M大小");
                return false;
            }

            if(this.queue.length > $scope.queueLimit){
                toaster.pop('waiting', "添加图片失败", "超出图片上传数量");
                return false;
            }

            return true;
        }
    });



    // CALLBACKS
    uploader.onWhenAddingFileFailed = function(item,filter, options) {
    };
    uploader.onAfterAddingFile = function(fileItem) {
        if($scope.queueLimit<uploader.queue.length){
            toaster.pop('waiting', "添加图片失败", "超出图片上传数量");
            uploader.queue.splice(uploader.queue.indexOf(fileItem),1);
        }
    };

    uploader.onAfterAddingAll = function(addedFileItems) {
        if(!uploader.getNotUploadedItems().length){
            return;
        }
        uploader.uploadAll();
    };
    uploader.onBeforeUploadItem = function(item) {
    };
    uploader.onProgressItem = function(fileItem, progress) {
    };
    uploader.onProgressAll = function(progress) {
    };
    uploader.onSuccessItem = function(fileItem, response, status, headers) {
        switch (status){
            case 200:
                fileItem.fileName=response.data;
                fileItem.imgsrc=$scope.app.image.temp+response.data;

                $scope.index++;
                var beginSort=0;
                if($scope.goodsImages.length>0){
                    beginSort=$scope.goodsImages[$scope.goodsImages.length - 1].sort;
                }
                fileItem.sort=beginSort+$scope.index;
                $scope.goodsDetailPicturesService.addGoodsImages.push(fileItem);


                break;
            default :
                break;
        }

    };
    uploader.onErrorItem = function(fileItem, response, status, headers) {
        toaster.pop('waiting', "上传图片失败", "未知错误");
    };
    uploader.onCancelItem = function(fileItem, response, status, headers) {
    };
    uploader.onCompleteItem = function(fileItem, response, status, headers) {
    };
    uploader.onCompleteAll = function() {
    };

    $scope.del=function(item){
        goodsFileUploadService.del($scope,item).then(function(result){
            switch (result.status){
                case 200:
                    item.remove();
                    var index=$scope.goodsDetailPicturesService.addGoodsImages.indexOf(item);
                    $scope.goodsDetailPicturesService.addGoodsImages.splice(index,1);
                    break;
                default :
                    break;
            }
        });
    }

    $scope.delError=function(item){
        item.remove();
    }


    $scope.init=function(limit,imagesType){
        $scope.queueLimit=$scope.scqueueLimit=limit;
        $scope.Imagestype=imagesType;
    }

    //监听父容器广播的数据
    $scope.$on('goodsImages',function(e,msg){
        if(angular.isUndefined(msg) || msg==null){
            $scope.goodsImages=[];
            $scope.queueLimit=$scope.scqueueLimit-$scope.goodsImages.length;
            return;
        }

        //获取到父控制器广播的内容；
        $scope.goodsImages=angular.isUndefined(msg[$scope.Imagestype])?[]:msg[$scope.Imagestype];
        $scope.queueLimit=$scope.scqueueLimit-$scope.goodsImages.length;
    });


    $scope.openUrl=function(url){
        $window.open(url);
    }


}]);


//父域
app.controller('goodsDetailPicturesCtrl',['$scope','$stateParams','$state','toaster','goodsDetailPicturesService',function($scope,$stateParams,$state,toaster,goodsDetailPicturesService){

    $scope.goodsId=$stateParams.goodsId;


    //查询商品
    $scope.findByGoodsId=function(){
        goodsDetailPicturesService.findByGoodsId($scope).then(function(result){
            switch (result.status){
                case 200:

                    $scope.$broadcast('goodsImages',result.data);

                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        })
    }

    $scope.findByGoodsId();

    //添加图片
    $scope.addState=false;

    $scope.add=function(){
        if($scope.addState){
            return;
        }

        $scope.addState=true;

        goodsDetailPicturesService.add($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.addState=false;
                    $state.go('app.goods');
                    break;
                default :
                    $scope.addState=false;
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        })
    }

    //删除图片
    $scope.delState=false;

    $scope.deleteGoodsImage=function(id){
        if($scope.delState){
            return;
        }

        $scope.delState=true;
        goodsDetailPicturesService.deleteGoodsImage($scope,id).then(function(result){
            switch (result.status){
                case 200:
                    $scope.delState=false;
                    toaster.pop('success', "提示", "操作成功");
                    $scope.findByGoodsId();
                    break;
                case 649:
                    $scope.delState=false;
                    $scope.findByGoodsId();
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        })
    }


}])